﻿using System;
using System.Collections.Specialized;
using System.Web.UI;

using Microsoft.SharePoint.WebControls;

namespace Eneta.Portal.Forum.Common.Controls
{
    public abstract class OWSBase : OWSControl, IPostBackDataHandler
    {
        // Events
        public event EventHandler ValueChanged;

        // Methods
        protected OWSBase()
        {
        }

        public virtual bool LoadPostData(string postDataKey, NameValueCollection postCollection)
        {
            string str = this.Value;
            this.Value = postCollection[postDataKey];
            return (str != this.Value);
        }

        private void OnValueChanged(EventArgs e)
        {
            if (this.ValueChanged != null)
            {
                this.ValueChanged(this, e);
            }
        }

        public virtual void RaisePostDataChangedEvent()
        {
            this.OnValueChanged(EventArgs.Empty);
        }

        // Properties
        public virtual string Description
        {
            get
            {
                return ("" + this.ViewState["Description"]);
            }
            set
            {
                this.ViewState["Description"] = value;
            }
        }

        public string Display
        {
            get
            {
                return ("" + this.ViewState["Display"]);
            }
            set
            {
                this.ViewState["Display"] = value;
            }
        }

        protected string JavaScriptValue
        {
            get
            {
                return this.Value.Replace(@"\", @"\\").Replace("\"", "\\\"").Replace("'", "'").Replace("\r", "").Replace("\n", @"\n");
            }
        }

        public bool Required
        {
            get
            {
                if (this.ViewState["Required"] == null)
                {
                    this.Required = false;
                }
                return (bool)this.ViewState["Required"];
            }
            set
            {
                this.ViewState["Required"] = value;
            }
        }

        public virtual string Text
        {
            get
            {
                return this.Value;
            }
            set
            {
            }
        }

        public virtual string Value
        {
            get
            {
                return ("" + this.ViewState["Value"]);
            }
            set
            {
                this.ViewState["Value"] = value;
            }
        }
    }
}
